package com.ez.supervise.util;

import java.lang.reflect.Method;

import java.text.SimpleDateFormat;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;


public class MainUtils {
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * Byte.
     */
    public static Byte getByte(String value, Byte defaultValue) {
        try {
            return Byte.valueOf(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Byte getByte(String value) {
        return getByte(value, null);
    }

    /**
     * Short.
     */
    public static Short getShort(String value, Short defaultValue) {
        try {
            return Short.valueOf(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Short getShort(String value) {
        return getShort(value, null);
    }

    /**
     * Integer.
     */
    public static Integer getInt(String value, Integer defaultValue) {
        try {
            return Integer.valueOf(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Integer getInt(String value) {
        return getInt(value, null);
    }

    /**
     * Long.
     */
    public static Long getLong(String value, Long defaultValue) {
        try {
            return Long.valueOf(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Long getLong(String value) {
        return getLong(value, null);
    }

    /**
     * Float.
     */
    public static Float getFloat(String value, Float defaultValue) {
        try {
            return Float.valueOf(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Float getFloat(String value) {
        return getFloat(value, null);
    }

    /**
     * Double.
     */
    public static Double getDouble(String value, Double defaultValue) {
        try {
            return Double.valueOf(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Double getDouble(String value) {
        return getDouble(value, null);
    }

    /**
     * Character.
     */
    public static Character getChar(String value, Character defaultValue) {
        try {
            return Character.valueOf(value.charAt(0));
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Character getChar(String value) {
        return getChar(value, null);
    }

    /**
     * Boolean.
     */
    public static Boolean getBoolean(String value, Boolean defaultValue) {
        try {
            return Boolean.valueOf(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Boolean getBoolean(String value) {
        return getBoolean(value, Boolean.FALSE);
    }

    /**
     * Date.
     */
    public static Date getDate(String value, Date defaultValue,
        String datePattern) {
        try {
            return new SimpleDateFormat(datePattern).parse(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Date getDate(String value, Date defaultValue) {
        return getDate(value, defaultValue, DEFAULT_DATE_PATTERN);
    }

    public static Date getDate(String value) {
        return getDate(value, null);
    }

    /**
     * copy from src to dest.
     */
    public static void copy(Object src, Object dest) {
        Class srcClass = src.getClass();
        Class destClass = dest.getClass();

        for (Method method : srcClass.getMethods()) {
            //System.out.println(" ////");
            //System.out.println(method);
            if (method.getName().equals("getClass")) {
                continue;
            }

            if (isGetter(method)) {
                try {
                    //
                    Object result = method.invoke(src);

                    //System.out.println(result);
                    if (result == null) {
                        continue;
                    } else if (result instanceof Collection) {
                        if (((Collection) result).isEmpty()) {
                            continue;
                        }
                    } else if (result.getClass().isArray()) {
                        if (result instanceof Object[]) {
                            if (((Object[]) result).length == 0) {
                                continue;
                            }
                        } else if (result instanceof byte[]) {
                            if (((byte[]) result).length == 0) {
                                continue;
                            }
                        } else if (result instanceof short[]) {
                            if (((short[]) result).length == 0) {
                                continue;
                            }
                        } else if (result instanceof int[]) {
                            if (((int[]) result).length == 0) {
                                continue;
                            }
                        } else if (result instanceof long[]) {
                            if (((long[]) result).length == 0) {
                                continue;
                            }
                        } else if (result instanceof float[]) {
                            if (((float[]) result).length == 0) {
                                continue;
                            }
                        } else if (result instanceof double[]) {
                            if (((double[]) result).length == 0) {
                                continue;
                            }
                        } else if (result instanceof char[]) {
                            if (((char[]) result).length == 0) {
                                continue;
                            }
                        } else if (result instanceof boolean[]) {
                            if (((boolean[]) result).length == 0) {
                                continue;
                            }
                        }
                    }

                    //
                    String methodName = getter2Setter(method.getName());
                    Class methodParam = method.getReturnType();
                    Method setter = destClass.getMethod(methodName,
                            methodParam);
                    //System.out.println(methodName);
                    //System.out.println(methodParam);
                    //System.out.println(setter);
                    //
                    setter.invoke(dest, result);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

            //System.out.println(" ////");
        }
    }

    public static String m2f(String methodName) {
        String fieldName = null;

        if (methodName.startsWith("set") || methodName.startsWith("get")) {
            fieldName = methodName.substring(3, 4).toLowerCase()
                + methodName.substring(4);
        } else if (methodName.startsWith("is")) {
            fieldName = methodName.substring(2, 3).toLowerCase()
                + methodName.substring(3);
        }

        return fieldName;
    }

    public static String getter2Setter(String methodName) {
        if (methodName.startsWith("get")) {
            return "s" + methodName.substring(1);
        } else if (methodName.startsWith("is")) {
            return "set" + methodName.substring(2);
        } else {
            return methodName;
        }
    }

    /**
     * 方法名是3个字符以上
     * 方法名以set开头
     * 方法只有一个参数
     *
     * 满足这三个条件，就被认为是setter
     */
    public static boolean isSetter(Method method) {
        String name = method.getName();

        return (name.length() > 3) && name.startsWith("set")
        && (method.getParameterTypes().length == 1);
    }

    /**
     * 方法名以get开头，并在3个字符以上
     * 方法名以is开头，并在2个字符以上
     * 方法没有参数
     *
     * 满足这三个条件，就被认为是getter
     */
    public static boolean isGetter(Method method) {
        String name = method.getName();

        if ((method.getParameterTypes().length == 0)
                && (((name.length() > 3) && name.startsWith("get"))
                || ((name.length() > 2) && name.startsWith("is")))) {
            return true;
        } else {
            return false;
        }
    }
}
